package com.mhc.haval.handler;

import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.subaru.common.entity.BizResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.URLDecoder;

/**
 * 定义全局异常处理
 * @author kanon
 * @RestControllerAdvice 是@controlleradvice 与@ResponseBody 的组合注解
 */
@RestControllerAdvice
@Slf4j
public class GlobalControllerExceptionHandler {

    private URLDecoder urlDecoder;

    @ExceptionHandler(value = {SuzukiCommonException.class})
    @ResponseStatus(HttpStatus.OK)
    public BizResult suzukiCommonException(SuzukiCommonException e) {
        log.error("获取到一个SuzukiCommonException异常:{}", e.getMessage());
        return BizResult.create(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = {HavalCommonException.class})
    @ResponseStatus(HttpStatus.OK)
    public BizResult havalCommonException(HavalCommonException e) {
        log.error("获取到一个HavalCommonException异常:{}", e.getMessage());
        return BizResult.create(e.getCode(), e.getMessage());
    }


    @ExceptionHandler(value = {RuntimeException.class})
    @ResponseStatus(HttpStatus.OK)
    public BizResult runtimException(RuntimeException e) {
        log.error("获取到一个RuntimeException异常:{}", e);
        return BizResult.create(MsgCodeEnum.EXCEPTION.getErrCode(), MsgCodeEnum.EXCEPTION.getErrMsg());
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public BizResult noHandlerdException(Exception e) {
        log.error("获取到一个Exception异常:{}", e);
        return BizResult.create(MsgCodeEnum.EXCEPTION.getErrCode(), MsgCodeEnum.EXCEPTION.getErrMsg());
    }

}